<template>
	<statusBarVue title="我的证书" :isShowBar="true" :isShowLeft="true" :isMyBgi="true" />
	<view class="documents-page" :style="pageHaveBarStyle">
		<view class="documents-page-group">
			<uni-swipe-action ref="swipeAction">
				<view class="documents-page-group-item" v-for="(v, i) in documentsList" :key="'document-'+i">
					<uni-swipe-action-item :right-options="optionsRight" @click="handleDelDocument(v.id)">
						<view class="documents-page-item">
							<view class="documents-page-item-img">
								<image :src="v.photo" @click="handlePreviewImage(v.photo)" />
							</view>
							<view class="documents-page-item-text">
								<view> <text>证书名称：</text> {{v.name}}</view>
								<view> <text>组织名称：</text> {{v.organization}}</view>
								<view><text>发行时间：</text> {{v.issue_date}}</view>
								<view><text>到期时间：</text> {{v.expire_date}}</view>
							</view>
						</view>
					</uni-swipe-action-item>
				</view>
			</uni-swipe-action>
		</view>

		<view class="documents-page-group  add-btns">
			<button style="color:#ffffff;backgroundColor:#22A366;" @click="handleDocument(undefined)">添加证书</button>
		</view>
	</view>
</template>

<script>
	import statusBarVue from '@/components/statusBar.vue'

	import {
		getCheckDocumentsList,
		postDelDocument,
		getStsRequest
	} from '@/utils/api.js'

	import {
		publics
	} from '@/mixins/publics.js';

	export default {
		mixins: [publics],
		data() {
			return {
				optionsRight: [{
					text: '删除',
					style: {
						backgroundColor: '#ff3f3f'
					}
				}],
				documentsList: []
			}
		},
		onShareAppMessage(){
			// 监听用户右上角的转发
		},
		onShareTimeline(){
			// 监听用户右上角的分享到朋友圈
		},
		components: {
			statusBarVue,
		},
		onShow() {
			this.getCheckDocumentsListApi();
		},
		methods: {
			// 查询列表
			getCheckDocumentsListApi() {
				this.$refs.swipeAction.closeAll();
				getCheckDocumentsList().then(req => {
					if (req.code === 200) {
						this.documentsList = req.data.documents;
					}
				});
			},

			// 添加证书
			handleDocument(id) {
				uni.navigateTo({
					url: id ? `/myPages/documents/add?id=${id}` : `/myPages/documents/add`
				});
			},

			// 删除列表
			postDelDocumentApi(id) {
				postDelDocument(id).then(res => {
					if (res.code === 200) {
						this.getCheckDocumentsListApi();
					}
				})
			},

			// 删除
			handleDelDocument(id) {
				uni.showModal({
					title: '提示',
					content: '您确定要删除此证书吗？',
					success: (res) => {
						if (res.confirm) {
							this.postDelDocumentApi(id)
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.documents-page {
		position: fixed;
		left: 0;
		right: 0;
		background-color: #F2F3F7;
		padding: 10px 0;
		box-sizing: border-box;
		z-index: 99;

		&-group {
			width: 94.67vw;
			margin: auto;
			font-size: 16px;

			&-item {
				padding: 12px 10px;
				background-color: #fff;
				margin-bottom: 15px;
				font-weight: bolder;
				box-sizing: border-box;

				.documents-page-item {
					display: flex;

					&-img {
						overflow: hidden;
						margin-right: 8px;

						image {
							width: 100px;
							height: 100px;
							border-radius: 100px;
						}
					}

					&-text {
						display: grid;
						align-content: space-between;
					}
				}


				text {
					font-weight: normal;
				}

				.date {
					display: flex;
					justify-content: space-between;
				}
			}
		}
	}
</style>